#-*- coding: utf-8 -*-
'''
Created on Aug 27, 2012

@author: bbg
'''

from sqlalchemy import Integer, Column, String, MetaData, Table
from sqlalchemy.orm import mapper
    
class ConfigFloor(object):

    _Mapper_ = None
    _company_id_ = -1
    _shop_id_ = -1
    def __init__(self):
        self.floor_name = ''
        self.rank_id = 0
        
    @staticmethod
    def doMapping(company_id, shop_id):
        '''
        按company_id分库，shop_id对应公司分店，每次生成对象前都要执行一次，以便map到正确的数据库上去
        '''
        if ConfigFloor._company_id_ == company_id and ConfigFloor._shop_id_ == shop_id:
            return
                
        if ConfigFloor._company_id_ == company_id and ConfigFloor._shop_id_ == shop_id:
            #has been mapped last time
            return 

        if ConfigFloor._Mapper_ != None:
            ConfigFloor._Mapper_.dispose()
            
        metaData = MetaData()
        configFloorTable = Table('config_floor', metaData,
                           Column('id', Integer, primary_key = True),
                           Column('floor_name', String),
                           Column('rank_id', Integer),
                           schema = "shop_%s_%s" % (company_id, shop_id))
        
        ConfigFloor._Mapper_ = mapper(ConfigFloor, configFloorTable)
    